package bigoo.ds;

import static java.util.logging.Logger.GLOBAL_LOGGER_NAME;

import java.io.IOException;
import java.util.logging.Logger;

import bigoo.common.communication.MessageBuilderFactory;
import bigoo.common.communication.tcp.TCPCommunication;
import bigoo.peer.message.GetPeersMessage;
import bigoo.peer.message.ImAliveMessage;
import bigoo.util.Config;

public class DSRunner {

    /**
     * @param args
     * @throws IOException
     * @throws SecurityException
     */
    public static void main(String[] args) throws SecurityException, IOException {
	Config.loadProperties(Config.DS_PROPERTIES);
	Config.configureLog();
	Logger logger = Logger.getLogger(GLOBAL_LOGGER_NAME);

	// parsing args and starting
	int port = Integer.parseInt(Config.getProperties().getProperty(Config.DS_PORT));
	logger.config("port => " + port);

	MessageBuilderFactory factory = MessageBuilderFactory.getInstance();
	factory.registerBuilder(ImAliveMessage.MSG_IDENTIFIER, new ImAliveMessage.Builder());
	factory.registerBuilder(GetPeersMessage.MSG_IDENTIFIER, new GetPeersMessage.Builder());

	new DiscoveryService(new TCPCommunication(port));
	logger.info("DS is UP and RUNNING!");
    }
}
